<?php
namespace Api\Controller;
use Think\Controller;
class UserController extends HomeController {
	// 注册
    public function register(){
    	$mobile = I('request.mobile', 'htmlspecialchars');
    	$password = I('request.password', 'htmlspecialchars');
    	$repassword = I('request.repassword', 'htmlspecialchars');
    	$ver_num = I('request.ver_num', 'htmlspecialchars');
    	$parent_id = I('request.parent_id', 'htmlspecialchars');
    	if (empty($mobile) || empty($password) || empty($repassword) || empty($ver_num) || empty($parent_id)){
    		error("有参数为空");
    	}
    	if(strlen($mobile)!=11)
    	{
    		error("请重新输入11位手机号码");
    	}
    	if($password != $repassword){
    		$this->error("两次输入密码不一致");
    	}
    	$mobile=trim($mobile);
    	
    	$arr_ver_info = _check_ver($mobile, 0, $ver_num);
    	
    	//判断用户是否已经存在
    	$arr_user_where = array (
    			'mobile' => $mobile,
    	);
    	$userObj = M("User");
    	$arr_user_info = $userObj->where($arr_user_where)->find();
    	if (is_array($arr_user_info))
    	{
    		error('手机号码已经存在');
    	}
    	
    	$parent_info = $userObj->find($parent_id);
    	if (empty($parent_info)){
    		error("推荐码不存在");
    	}
    	
    	$add_user_data = array(
    		'mobile' => $mobile,
    		'password' => xmd5($password),
    		'nickname' => $mobile,
    		'sex' => 0,
    		'headimg' => "/Public/default.png",
    		'reg_time' => time(),
    		'reg_ip' => get_client_ip(),
    	);
    	$user_id = $userObj->add($add_user_data);
    	
    	$userObj -> where('id='.$user_id) -> save(array(
    	'parent1' => $parent_info['id'],
    	'parent2' => $parent_info['parent1'],
    	'parent3' => $parent_info['parent2']
    	));
    	 
    	// 增加上级的统计
    	$userObj -> where('id='.$parent_info['id']) -> setInc('agent1');
    	$userObj -> where('id='.$parent_info['parent1']) -> setInc('agent2');
    	$userObj -> where('id='.$parent_info['parent2']) -> setInc('agent3');
    	 
    	//依次更新上级等级
    	update_level($parent_info, $this ->_level);
    	update_level($parent_info['parent1'], $this ->_level);
    	update_level($parent_info['parent2'], $this ->_level);
    	success("注册成功");
    }
    // 登录
    public function login(){
    	$mobile = I('request.mobile', 'htmlspecialchars');
    	$password = I('request.password', 'htmlspecialchars');
    	$device_id = I('request.device_id', 'htmlspecialchars');
    	if (empty($mobile) || empty($password) || empty($device_id)){
    		error("有参数为空");
    	}
    	$arr_login_data = array (
    			'mobile' => $mobile,
    			'password' => xmd5($password),
    	);
    	$arr_re = M("User")->where($arr_login_data)->find();
    	if ($arr_re)
    	{
    		if ($arr_re['status'] == 0)
    		{
    			// 更新登录状态
    			$userLoginObj=M("User_login");
    			$login_where['user_id'] = $arr_re['id'];
    			$userLoginObj->where($login_where)->delete();
    			$login_data = array(
    				'user_id'=>$arr_re['id'],
    				'login_time'=>time(),
    				'login_ip'=>get_client_ip(),
    				'device_id'=>$device_id,
    				'token'=>md5($arr_info['mobile'].$arr_info['password'].time()),
    			);
    			$result = $userLoginObj->add($login_data);
    			if ($result!=false){
    				$data = array(
    					'token'=>$login_data['token'],
    				);
    				success($data);
    			}else{error("数据库错误",2);}
    		}
    		else{error('用户状态异常');}
    	}
    	else{error('用户名或者密码错误', 1);}
    }
    // 检查登录状态
    public function check_login(){
    	$token = I('request.token', 'htmlspecialchars');
    	if (empty($token)){
    		error("有参数为空");
    	}
    	$login_info = _check_login($token);
    	if($login_info){
    		success("已登录");
    	}
    }    
    // 退出登录
    public function login_out(){
    	$token = I('request.token', 'htmlspecialchars');
    	if (empty($token) || empty($device_id)){
    		error("有参数为空");
    	}
    	$login_where = array(
    			'token' => $token,
    	);
    	
    	$login_info = _check_login($token);
    	$user_info = _check_user_by_id($login_info['user_id']);
    	
    	$userLoginObj=M("User_login");
    	$result = $userLoginObj->where($login_where)->delete();
    	if($result!=false){
    		success("成功");
    	}else{
    		error("失败");
    	}
    }
    // 获取用户信息
    public function get_user_info(){
    	$token = I('request.token', 'htmlspecialchars');
    	if (empty($token)){
    		error("有参数为空");
    	}
    	$login_info = _check_login($token);
    	$user_info = _check_user_by_id($login_info['user_id']);
    	
    	$user_info['cardno'] = get_encrypt_idcard($user_info['cardno']);
    	$user_info['true_name'] = get_encrypt_true_name($user_info['true_name']);
    	$user_info['mobile'] = get_encrypt_mobile($user_info['mobile']);
    	$user_info['sex_name'] = get_sex_name($user_info['sex']);
    	$user_info['status_name'] = get_user_status($user_info['status']);
    	// 获取上家昵称
    	if(!empty($user_info['parent1'])){
    		$parent_name = M('user') -> where("id=".$user_info['parent1']) -> getField('nickname');
    		$user_info['parent_name'] = $parent_name;
    	}
    	// 获取等级名称数组
    	$user_info['level_name'] = $this->_level[$user_info['level']]['name']; // 我的等级名称
    	// 去除记录中的关键信息
    	unset($user_info['password']);
    	success($user_info);
    }
    // 找回密码
    public function find_password(){
    	$mobile = I('request.mobile', 'htmlspecialchars');
    	$password = I('request.password', 'htmlspecialchars');
    	$repassword = I('request.repassword', 'htmlspecialchars');
    	$ver_num = I('request.ver_num', 'htmlspecialchars');
    	if (empty($mobile) || empty($password) || empty($repassword) || empty($ver_num)){
    		error("有参数为空");
    	}
    	if(strlen($mobile)!=11)
    	{
    		error("请重新输入11位手机号码");
    	}
    	if($password != $repassword){
    		$this->error("两次输入密码不一致");
    	}
    	$mobile=trim($mobile);
    	$arr_ver_info = _check_ver($mobile, 7, $ver_num);
    	//判断用户是否存在
    	$arr_user_where = array (
    			'mobile' => $str_mobile,
    	);
    	$userObj = M("User");
    	$arr_user_info = $userObj->where($arr_user_where)->find();
    	if ($arr_user_info)
    	{
    		$arr_save_data = array (
    				'password' => xmd5($password)
    		);
    		$result = $userObj->where($arr_user_where)->save($arr_save_data);
    		if ($result !== false)
    		{
    			success();
    		}
    		else
    		{
    			error('db error', 2);
    		}
    	}else{
    		error("用户不存在");
    	}
    }
    // 修改密码
    public function change_password(){
    	$token = I('request.token', 'htmlspecialchars');
    	$password = I('request.password', 'htmlspecialchars');
    	$repassword = I('request.repassword', 'htmlspecialchars');
    	$ver_num = I('request.ver_num', 'htmlspecialchars');
    	if (empty($token) || empty($password) || empty($repassword) || empty($ver_num)){
    		error("有参数为空");
    	}
    	if($password != $repassword){
    		$this->error("两次输入密码不一致");
    	}
    	
    	$login_info = _check_login($token);
    	$user_info = _check_user_by_id($login_info['user_id']);   	 	
    	$arr_ver_info = _check_ver($user_info['mobile'], 5, $ver_num);
    	
    	$userObj = M("User");
    	$arr_save_data = array (
    			'password' => xmd5($password)
   		);
    	$arr_user_where = array (
    			'id' => $user_info['id'],
    	);
   		$result = $userObj->where($arr_user_where)->save($arr_save_data);
   		if ($result !== false)
   		{
   			success();
    	}
   		else
   		{
   			error('db error', 2);
   		}
    	  	
    }
    // 修改用户信息
    public function edit_user_info(){
    	$token = I('request.token', 'htmlspecialchars');
    	$mobile = I('request.mobile', 'htmlspecialchars');
    	$headimg = $_FILES['headimg'];
    	$nickname = I('request.nickname', 'htmlspecialchars');
    	$sex = I('request.sex', 'htmlspecialchars');
    	$true_name = I('request.true_name', 'htmlspecialchars');
    	$cardno = I('request.cardno', 'htmlspecialchars');
    	$birth = I('request.birth', 'htmlspecialchars');
    	$ver_num = I('request.ver_num', 'htmlspecialchars');
    	
    	if(empty($token) || empty($ver_num)){
    		error("有参数为空");
    	}
    	$login_info = _check_login($token);
    	$user_info = _check_user_by_id($login_info['user_id']);   	 	
    	$arr_ver_info = _check_ver($user_info['mobile'], 6, $ver_num);
    	
    	$userObj = M("User");
    	// 如果手机号不为空，并且不等于当前数据库中的手机号，说明需要修改，验证要更改的手机号是否已经存在
   		if(!empty($mobile) && $mobile!=$user_info['mobile']){
   			$check_mobile_where['mobile'] = $mobile;
   			$check_mobile_where['id'] = array('neq',$user_info['id']);
   			$check_user_info = $userObj->where($check_mobile_where)->find();
   			if(!empty($check_user_info)){
   				error("手机号已经存在");
   			}
   			$arr_save_data['mobile'] = $mobile;
   		}
   		// 如果有上传头像
   		if (!empty($headimg['name']))
   		{
   			if ($headimg['error'] != 0)
   			{
   				error('图片上传失败', 3);
   			}
   			else
   			{
   				$u_result = _upload($headimg);
   				if($u_result==0){
   					error("头像上传错误");
   				}elseif ($u_result==1){
   					error("保存头像图片文件时出错");
   				}
   				$arr_save_data['headimg'] = $u_result;
   			}
   		}
   		// 是否需要修改昵称
   		if(!empty($nickname)){
   			$arr_save_data['nickname'] = $nickname;
   		}
   		// 是否需要修改性别
   		if($sex!=null){
   			$arr_save_data['sex'] = $sex;
   		}
   		// 是否需要修改真实姓名
   		if(empty($true_name)){
   			$arr_save_data['true_name'] = $true_name;
   		}
   		// 是否需要修改身份证号码
   		if(empty($true_name)){
   			$arr_save_data['cardno'] = $cardno;
   		}
   		// 是否需要修改生日
   		if(empty($true_name)){
   			$arr_save_data['birth'] = $birth;
   		}
   		$arr_user_where = array (
   				'id' => $user_info['id'],
   		);
   		$result = $userObj->where($arr_user_where)->save($arr_save_data);
   		if ($result !== false)
   		{
   			success();
   		}
   		else
   		{
   			error('db error', 2);
   		}
   }
}